from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView
from . seriazer import *
from . import models
import re
class GredaView(APIView):
    def post(self,request):
        gname=request.data.get("gname")
        if Grade.objects.filter(gname=gname):
            return Response({"msg":"存在",'code':1001})
        obj=GradeModelSerializer(data=request.data)
        if obj.is_valid():
            flay=obj.save()
            if flay:
                return Response({"msg":"成功",'code':1000})
            return Response({"msg":"失败",'code':1002})

        print(obj.errors)
        return Response({"msg":"参数错误",'code':1003})

    def get(self,request):
        objall=models.Grade.objects.all()
        obj=GradeModelSerializer(objall,many=True)
        return Response({"code":1000,"data":obj.data})

    def delete(self,request):
        id =request.data.get('id')
        user_tuple=models.Grade.objects.filter(id=id).delete()
        if user_tuple[0]:
            return Response({"msg":"删除成功",'code':1000})
        return Response({"msg":"删除失败",'code':1001})



class UserView(APIView):
    def post(self,request):
        username=request.data.get("username")
        password=request.data.get("password")
        password2=request.data.get("password2")
        email=request.data.get("email")
        if not re.match(r'[[0-9a-zA-Z_]{0,19}@(.*?).com',email):
            return Response({'msg':'不符合','code':1004})
        if password==password2:
            user_obj=User.objects.filter(username=username)
            if user_obj:
                return Response({"msg":"数据存在",'code':1001})
            obj=UserModelSerializer(data=request.data)
            if obj.is_valid():
                flay=obj.save()
                if flay:
                    return Response({"msg":"注册成功",'code':1000})
                return Response({"msg":"注册失败",'code':1002})

            print(obj.errors)
            return Response({"msg":"参数错误",'code':1005})
        return Response({"msg":"两次密码不一致"})
    def get(self,request):
        objall=models.Grade.objects.all()
        obj=UserModelSerializer(objall,many=True)
        return Response({"code":1000,"data":obj.data})
    def delete(self,request):
        id =request.data.get('id')
        user_tuple=models.User.objects.filter(id=id).delete()
        if user_tuple[0]:
            return Response({"msg":"删除成功",'code':1000})
        return Response({"msg":"删除失败",'code':1001})

class LoginView(APIView):
    def post(self,request):
        username=request.data.get("username")
        password=request.data.get("password")
        obj=models.User.objects.filter(username=username).first()
        if obj:
            if obj.password==password:
                return Response({"msg":"登录成功",'code':1000,'user_id':obj.pk})
            else:
                return Response({"msg":"密码或用户名不正确",'code':1001})
        else:
            return Response({"msg":"密码或用户名不正确",'code':1001})

class UserViewShow(APIView):
    def get(self,request):
        userobj=models.User.objects.all()
        boj=UserModelSerializer(userobj,many=True)
        return Response(boj.data)


class Update_User(APIView):
    def post(self, request):
        uid = request.POST.get('uid')
        Grade_obj = Grade.objects.get(pk=uid)
        return Response("删除成功")



